﻿
using Model.Core;
using Models;
using SCADA_DAQ.ZEMT.Server;
using SIE.SCADA.SqlSugar;
using SIE.SCADA.ZemtDemo.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Microsoft.Office.Interop.Excel;
using System.IO;
using AduSkin.Controls.Metro;

using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using SIE.SCADA.Common.Log;

namespace SIE.SCADA.ZemtDemo.ViewModel
{
    public class TurqueRecordViewModel : QueryBaseViewModel<scada_torquerecord>
    {
        PgSqlAccess pgsql = new PgSqlAccess();

        public DateTime _starttime;
        public DateTime starttime
        {
            get { return _starttime; }
            set
            {
                if (value != _starttime)
                {
                    _starttime = value;
                    OnPropertyChanged(nameof(starttime));
                }
            }
        }

        public DateTime _endtime;
        public DateTime endtime
        {
            get { return _endtime; }
            set
            {
                if (value != _endtime)
                {
                    _endtime = value;
                    OnPropertyChanged(nameof(endtime));
                }
            }
        }



        public string _equipmentcode;
        public string equipmentcode
        {
            get { return _equipmentcode; }
            set
            {
                if (value != _equipmentcode)
                {
                    _equipmentcode = value;
                    OnPropertyChanged(nameof(equipmentcode));
                }
            }
        }

        public string _vinnum;
        public string vinnum
        {
            get { return _vinnum; }
            set
            {
                if (value != _vinnum)
                {
                    _vinnum = value;
                    OnPropertyChanged(nameof(vinnum));
                }
            }
        }


        public string _userid;
        public string userid
        {
            get { return _userid; }
            set
            {
                if (value != _userid)
                {
                    _userid = value;
                    OnPropertyChanged(nameof(userid));
                }
            }
        }

        public TurqueRecordViewModel()
        {
            //starttime = DateTime.Now.GetDayStart();
            //endtime = DateTime.Now.GetDayEnd();
        }

        public  void Query(string start , string end)
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_torquerecord>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    string sql = " 1=1 ";
                    if (vinnum != null && vinnum != "")
                    {
                        sql += " and  vinnum like  " + "'%" + vinnum + "%'";
                    }
                    
                    if (userid != null && userid != "")
                    {
                        sql += " and  userid like  " + "'%" + userid + "%'";
                    }

                    if (equipmentcode !=null && equipmentcode !="") {
                        sql += " and  equipmentcode like  " + "'%" + equipmentcode + "%'";
                    }
                    if (start.ToString() != "" && end.ToString() != "")
                    {
                        sql += " and  time between   " + " '" + start + "' " + " and " + " '" + end + "' ";
                    }

                    var temps = ormClient.SqlSugarClient.Queryable<scada_torquerecord>().Where(sql).ToList();




                    //Records = ormClient.SqlSugarClient.Queryable<scada_torquerecord>().ToList();

                    //var temps = ormClient.SqlSugarClient.Queryable<scada_jobstep_record>().Where(sql).ToList();


                    var equipments = ormClient.SqlSugarClient.Queryable<scada_equipment>().Where("1=1").ToList();

                    foreach (var record in temps)
                    {
                      

                        //设备名称
                        if (equipments != null)
                        {
                            string desc5 = equipments.Where(r => r.equipmentid == record.equipmentcode).FirstOrDefault().equipname;
                            record.equipmentname = desc5;

                        }



                    }


                    Records = temps;











                }
            }
        }
          



        internal void Export2()
        {
            //var record= Records;
            //System.Data.DataTable dt = new System.Data.DataTable();
            //dt.Columns.Add("ID", typeof(int));
            //dt.Columns.Add("NickName", typeof(string));
            //dt.Columns.Add("QNumber", typeof(string));

            //DataRow row = dt.NewRow();
            //row["ID"] = 1;
            //row["NickName"] = "sanjiawan";
            //row["QNumber"] = "12345678";
            //dt.Rows.Add(row);

            //row = dt.NewRow();
            //row["ID"] = 2;
            //row["NickName"] = "人物2";
            //row["QNumber"] = "9058307";
            //dt.Rows.Add(row);

            ////创建Excel

            //Application excelApp = new Application();
            //Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing);    //创建工作簿（WorkBook：即Excel文件主体本身）
            //Worksheet excelWS = (Worksheet)excelWB.Worksheets[1];   //创建工作表（即Excel里的子表sheet） 1表示在子表sheet1里进行数据导出

            ////excelWS.Cells.NumberFormat = "@";     //  如果数据中存在数字类型 可以让它变文本格式显示
            ////将数据导入到工作表的单元格
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    for (int j = 0; j < dt.Columns.Count; j++)
            //    {
            //        excelWS.Cells[i + 1, j + 1] = dt.Rows[i][j].ToString();   //Excel单元格第一个从索引1开始
            //    }
            //}

            //excelWB.SaveAs("D:\\sanjiawan.xlsx");  //将其进行保存到指定的路径
            //excelWB.Close();
            //excelApp.Quit();  //KillAllExcel(excelApp); 释放可能还没释放的进程
        }


        internal void Export(AduDataGrid TurqueRecord)
        {
            var dd = TurqueRecord;
            csvHelper.ExportDataGridSaveAs(true, TurqueRecord);
        }

        public void Query(string matid)
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_torquerecord>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    Records = ormClient.SqlSugarClient.Queryable<scada_torquerecord>().Where(m => m.matid== matid).ToList();
                }
            }
        }

        


        public override void Delete()
        {
            if (CurItem != null)
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Deleteable<scada_torquerecord>().Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                base.Delete();
            }
        }

        public override void Update()
        {
            if (CurItem !=null) {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Updateable(CurItem).Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                base.Update();
            }
            //if(string.IsNullOrEmpty(CurItem.equipmentid))
            //{
            //    equipManager.Request(CurItem.equipmentid);
            //}
        }

        

        public override void Add()
        {
            using (var ctx = pgsql.GetClient())
            {
                try
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        var equipment = new scada_torquerecord();
                        ormClient.SqlSugarClient.Insertable<scada_torquerecord>(equipment).IgnoreColumns(it => it == "id").ExecuteCommand();
                        Query();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message);
                    Logger.Error(ex.StackTrace);
                }
                base.Add();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateFromMq()
        {

        }

    }
}
